package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.console.excel.ExportExcel;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.voiceRoom.LiveConversionQueryDTO;
import com.qumitech.visualnovel.controller.vo.voiceRoom.VoiceRoomAnchorVO;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.*;
import com.qumitech.visualnovel.service.voiceRoom.VoiceRoomAnchorService;
import com.qumitech.visualnovel.service.voiceRoom.VoiceRoomConfigService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.management.LockInfo;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2019/4/17 16:19
 */

@RestController
@RequestMapping("/api/voiceroom")
public class VoiceRoomController {
    @Autowired
    VoiceRoomAnchorService voiceRoomAnchorService;
    @Autowired
    private VoiceRoomConfigService voiceRoomConfigService;

    @PutMapping("/anchor/{anchor_id}")
    public BaseRsp updateVoiceRoomAnchor(@PathVariable("anchor_id") String anchorId,
                                         @RequestBody VoiceRoomAnchorVO anchorVO) {
        anchorVO.setAnchorId(anchorId);
        voiceRoomAnchorService.updateAnchor(anchorVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @GetMapping("/anchor")
    public BaseRsp getVoiceRoomAnchor(@RequestParam(value = "type", required = false) Byte type,
                                      @RequestParam(value = "status", required = false) Byte status,
                                      @RequestParam(value = "uid", required = false) Long uid,
                                      @RequestParam(value = "offset", defaultValue = "0") Integer offset,
                                      @RequestParam(value = "limit", defaultValue = "20") Integer limit) {

        return new BaseRsp(ResultEnum.SUCCESS, voiceRoomAnchorService.listVoiceRoomAnchorVO(uid, type, status, offset, limit));
    }

    @GetMapping("/top/config")
    public BaseRsp getTopVoiceRoom(VoiceRoomSearchDTO searchDTO){
        return new BaseRsp(ResultEnum.SUCCESS, voiceRoomConfigService.getTopVoiceRoom(searchDTO));
    }

    @PostMapping("/top/config")
    public BaseRsp addTopVoiceRoom(@RequestBody TopVoiceRoomDTO body){
       return voiceRoomConfigService.addTopVoiceRoom(body);

    }

    @PutMapping("/top/config/{config_id}")
    public BaseRsp updateTopVoiceRoom(@PathVariable("config_id") Long configId,
                                      @RequestBody TopVoiceRoomDTO body){

        return   voiceRoomConfigService.updateTopVoiceRoom(configId, body);

    }

    @DeleteMapping("/top/config/{config_id}")
    public BaseRsp deleteTopVoiceRoom(@PathVariable("config_id") Long configId){
        return  voiceRoomConfigService.deleteTopVoiceRoom(configId);
    }

    @GetMapping()
    public BaseRsp getRoomList(@RequestParam(name = "query", required = false) String query,
                               @RequestParam(name = "limit", required = false, defaultValue = "20") Integer limit,
                               @RequestParam(name = "offset", required = false, defaultValue = "0") Integer offset){
        VoiceRoomSearchDTO searchDTO = new VoiceRoomSearchDTO();
        searchDTO.setQuery(query);
        searchDTO.setLimit(limit);
        searchDTO.setOffset(offset);
        return new BaseRsp(ResultEnum.SUCCESS, voiceRoomConfigService.getVoiceRoomList(searchDTO));
    }

    @GetMapping("/short_id")
    public BaseRsp getVoiceRoomShortIdList(VoiceRoomShortIdAdminQueryDTO shortIdAdminQueryDTO){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("limit", shortIdAdminQueryDTO.getLimit().toString());
        paramMap.put("offset", shortIdAdminQueryDTO.getOffset().toString());
        if (shortIdAdminQueryDTO.getUid() != null){
            paramMap.put("uid", shortIdAdminQueryDTO.getUid().toString());
        }
        if (shortIdAdminQueryDTO.getShortId() != null){
            paramMap.put("short_id", shortIdAdminQueryDTO.getShortId());
        }
        return new BaseRsp(ResultEnum.SUCCESS, voiceRoomAnchorService.listExistShortIdVoiceRoom(paramMap));
    }

    @PostMapping("/short_id")
    public BaseRsp addVoiceRoomShortId(@RequestBody VoiceRoomShortIdAdminDTO shortIdAdminDTO){
        voiceRoomAnchorService.addVoiceRoomShortId(shortIdAdminDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/short_id")
    public BaseRsp deleteVoiceRoomShortId(@RequestParam("uid") Long uid){
        voiceRoomAnchorService.deleteVoiceRoomShortIdByUid(uid);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/white/config")
    public BaseRsp getVoiceRoomWhiteList(VoiceRoomWhiteCfgAdminQueryDTO queryDTO){
        return voiceRoomAnchorService.listVoiceRoomWhiteCfgAdminQueryDTO(queryDTO);
    }

    @PostMapping("/white/config")
    public BaseRsp addVoiceRoomShortId(@RequestBody VoiceRoomWhiteCfgDTO whiteCfgDTO){
        return voiceRoomAnchorService.addVoiceRoomWhiteCfg(whiteCfgDTO);
    }

    @DeleteMapping("/white/config/{roomId}")
    public BaseRsp rmVoiceRoomWhiteCfgDTO(@PathVariable("roomId") String roomId){
        return voiceRoomAnchorService.rmVoiceRoomWhiteCfg(roomId);
    }

    @GetMapping("rebate_type/option")
    public BaseRsp getRebateTypeOption()
    {
        return voiceRoomAnchorService.getRebateTypeOption();
    }

    @GetMapping("/live/conversion")
    public BaseRsp getLiveConversionStatistics(LiveConversionQueryDTO queryDTO) {

        return voiceRoomAnchorService.getLiveConversionStatistics(queryDTO);
    }

    @GetMapping("/activity")
    public BaseRsp getActivities(@RequestParam(value = "activityName", required = false) String activityName,
                                 @RequestParam(value = "streamerId", required = false) Long streamerId,
                                 @RequestParam(value = "startTime", required = false) String startTime,
                                 @RequestParam(value = "endTime", required = false) String endTime,
                                 @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
                                 @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
                                 @RequestParam(value = "orderBy", required=false, defaultValue = "start_time") String orderBy,
                                 @RequestParam(value = "orderType", required=false, defaultValue = "DESC") String orderType
                                 ) {

        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", voiceRoomAnchorService.getActivitiesListCount(activityName, streamerId, startTime, endTime),
                "items", voiceRoomAnchorService.getActivitiesList(activityName, streamerId, startTime, endTime, offset, limit, orderBy, orderType)));
    }

    @PostMapping("/activity")
    public BaseRsp updateActivity(@RequestBody List<ActivityConfigDTO> activityDTO) {
        int status = voiceRoomAnchorService.updateActivity(activityDTO);
        switch (status) {
            case 1:
                return new BaseRsp(ResultEnum.SUCCESS);
            case 0:
                return new BaseRsp(401, "参数不全");
            case 2:
                return new BaseRsp(402, "活动名称不能重复，请重新填写");
        }
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PostMapping("/activity/disable")
    public BaseRsp deleteActivity(@RequestBody Map<String, String> map) {
        voiceRoomAnchorService.deleteActivity(map);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/activity/user")
    @ExportExcel(itemKeys = {"account_id","present_id","user_name","vip_grade","register_time","last_active_time","recharge","diamond_consumed","hicoin_consumed"},
            itemNames = {"内部ID","显示ID","昵称","等级","注册时间","最后活跃时间","商城充值/元","钻石消耗/钻","嗨币消耗/钻"},
            listKey = "items")
    public BaseRsp getActivityUsersList(@RequestParam(value = "activityName") String activityName,
                                     @RequestParam(value = "streamerId", required = false) Long streamerId,
                                     @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
                                     @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
                                     @RequestParam(value = "orderBy", required=false, defaultValue = "recharge") String orderBy,
                                     @RequestParam(value = "orderType", required=false, defaultValue = "DESC") String orderType) {
        HashMap<String,Object> data = voiceRoomAnchorService.getActivityUsersListCount(activityName, streamerId).get(0);
        Object count = data.get("count");
        data.remove("count");
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count,
                "items", voiceRoomAnchorService.getActivityUsersList(activityName, streamerId, offset, limit, orderBy, orderType),
                "total", data));
    }

    @GetMapping("/activity/remain")
    public BaseRsp getActivityRemainList(@RequestParam(value = "activityName") String activityName,
                                     @RequestParam(value = "streamerId", required = false) Long streamerId,
                                     @RequestParam(value = "offset", required = false, defaultValue = "0") Integer offset,
                                     @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
                                     @RequestParam(value = "orderBy", required=false, defaultValue = "account_id") String orderBy,
                                     @RequestParam(value = "orderType", required=false, defaultValue = "DESC") String orderType) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("items", voiceRoomAnchorService.getActivityRemainList(activityName, streamerId, offset, limit, orderBy, orderType)));
    }

}
